package nc.lfw.finance.print;

import java.util.HashMap;
import java.util.Map;

import nc.lfw.data.Dataset;
import nc.lfw.data.Field;
import nc.lfw.data.RowSet;
import nc.lfw.finance.util.JkBxUtil;
import nc.ui.pub.print.IDataSource;
import nc.vo.pub.lang.UFDouble;

/**
 * @author luoyf
 * @version V5.7
 */
public class ReportPrintDatasource implements IDataSource {
	private static final long serialVersionUID = 1L;

	private Dataset masterDs = null;
	private Dataset slaveDs = null;
	private String extendKeys = null;
	private String extendValues = null;
	private String[][] precisionArr;
	
	private Map<String,String> qryObjMap = new HashMap<String,String>();

	public ReportPrintDatasource() {
		String[] precision = JkBxUtil.getPrecision();
		precisionArr = new String[][] { { "debitamount","debitfracamount","debitlocalamount"}, { precision[0],precision[0],precision[0]} };
	}

	protected Object processValue(Field field, String attrName, Object value) {
		for (int i = 0; i < precisionArr[0].length; i++) {
			if (attrName.equals(precisionArr[0][i])) {
				UFDouble dValue = null;
				if (!(value instanceof UFDouble))
					dValue = new UFDouble(value.toString());
				dValue = dValue.setScale(Integer.parseInt(precisionArr[1][i]),
						UFDouble.ROUND_HALF_UP);
				return dValue;
			}
		}
		return value;
	}
	
	public void setDatasets(Dataset masterDs, Dataset slaveDs, String extendKeys, String extendValues){
		this.extendKeys = extendKeys;
		this.extendValues = extendValues;
		this.masterDs = masterDs;
		this.slaveDs = slaveDs;
		
		String[] keys = this.extendKeys.split(",");
		String[] values = this.extendValues.split(",");
		for(int i=0;i<keys.length;i++)
			qryObjMap.put(keys[i],values[i]);
	}

	public String[] getAllDataItemExpress() {
		return null;
	}

	public String[] getAllDataItemNames() {
		return null;
	}

	public String[] getDependentItemExpressByExpress(String itemExpress) {
		if(itemExpress.equals("QryvalueName"))
			return new String[]{"QryobjectName"};
		return null;
	}
	

	public String[] getItemValuesByExpress(String itemExpress) {
		Dataset ds = null;
		String itemKey = itemExpress;
		if(itemExpress.startsWith("h_") || itemExpress.startsWith("t_")){
			itemKey = itemExpress.substring(2);
			ds = this.masterDs;
			if(itemKey.startsWith("qryobjname")){
				return new String[]{qryObjMap.get(itemKey)};
			}
		}
		else{
			ds = this.slaveDs;
			if(itemKey.equals("QryobjectName")){
				if(extendValues == null)
					return null;
				return extendValues.split(",", -1);
			}
			else if(itemKey.equals("QryvalueName")){
				if(extendKeys == null)
					return null;
				String[] keys = extendKeys.split(",", -1);
				RowSet rowSet = ds.getRowSet();
				if(rowSet != null && rowSet.size() > 0){
					int rowCount = rowSet.size();
					String[] values = new String[rowCount * keys.length];
					for (int i = 0; i < rowCount; i++) {
						for(int j = 0; j < keys.length; j ++){
							int fieldIndex = ds.getFieldSet().nameToIndex(keys[j]);
							if(fieldIndex != -1)
								values[i * keys.length + j] = (String) rowSet.getRow(i).getValue(fieldIndex);
						}
					}
					return values;
				}
				
				return null;
			}
			
		}
		
			
		//String attrName = itemExpress;
		RowSet rowSet = ds.getRowSet();
		if(rowSet != null && rowSet.size() > 0){
			int rowCount = rowSet.size();
			String[] values = new String[rowCount];
			for (int i = 0; i < rowCount; i++) {
				int fieldIndex = ds.getFieldSet().nameToIndex(itemKey);
				if(fieldIndex != -1){ 
					Field field = ds.getFieldSet().getField(fieldIndex);
					Object obj = rowSet.getRow(i).getValue(fieldIndex);
					if(obj != null){
						Object obj1 = processValue(field, itemKey, obj);
						values[i] = obj1.toString();
					}
					   
				}
					
			}
			return values;
		}
		
		return null;
	}

	public String getModuleName() {
		return null;
	}

	public boolean isNumber(String itemExpress) {
		return false;
	}

}
